package com.xiyue.leaspring.action;


import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/webobj")
@Slf4j
public class WebObjectAction {

    @RequestMapping("/showone")
    public ModelAndView showOne(HttpServletRequest request, HttpServletResponse response){
        log.info("**【REQUEST内置对象】ContextPath="+request.getContextPath());
        log.info("**【APPLICATION内置对象】RealPaTH="+request.getSession().getServletContext().getRealPath("/"));
        log.info("**【RESPONSE内置对象】Locale="+response.getLocale());
        log.info("**【SESSION内置对象】SessionID="+request.getSession().getId());
        return null;
    }

    @RequestMapping("/showtwo")
    public ModelAndView showTwo(){
        //通过RequestContextHolder类可以获得HttpServletRequest与HttpServletResponse
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpServletResponse response = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
        HttpSession session = request.getSession();
        ServletContext context = request.getSession().getServletContext();
        log.info("**【REQUEST内置对象】ContextPath="+request.getContextPath());
        log.info("**【APPLICATION内置对象】RealPaTH="+request.getSession().getServletContext().getRealPath("/"));
        log.info("**【RESPONSE内置对象】Locale="+response.getLocale());
        log.info("**【SESSION内置对象】SessionID="+request.getSession().getId());
        return null;
    }
}
